#include "MyCamera.h"


MyQcamera::MyQcamera(QObject *parent) : QObject(parent)
{
    flushCamera();
    //默认初始化QCamera使用列表第一个摄像头设备
    camera = new QCamera(cameras.first());
    //初始化QCameraViewfinder
    viewfinder = new QCameraViewfinder();
    //用于将摄像头的预览视图设置为指定的视图部件，以便实时显示摄像头捕获到的图像
    camera->setViewfinder(viewfinder);
    //更改当前使用摄像头
    if(!cameras.isEmpty())
    {
        currentCameraInfo = new QCameraInfo(cameras.first());
    }

     //currentCameraInfo = &*(cameras.begin() + 1);
    connect(camera, SIGNAL(stateChanged(QCamera::State)), this, SLOT(cameraStat(QCamera::State)));
    //启动摄像头
    camera->start();
}

void MyQcamera::cameraStat(QCamera::State state)
{
    switch(state)
    {
    //表示当前摄像头未被加载或者被关闭
    case QCamera::State::UnloadedState:
        ILOG(tr("摄像头：%1 未加载").arg(currentCameraInfo->description()));
        break;
    //表示当前摄像头已加载但是未处在活跃状态，即没有开始捕获视频或者图像
    case QCamera::State::LoadedState:
         ILOG(tr("摄像头：%1 已加载").arg(currentCameraInfo->description()));
        break;
    //表示当前摄像头处于活跃状态，正在捕获视频或者图像
    case QCamera::State::ActiveState:
         ILOG(tr("摄像头：%1 开始运行").arg(currentCameraInfo->description()));
        break;
    }

}

void MyQcamera::flushCamera()
{
    cameras.clear();
    //获取当前系统所有能用的摄像头，并存储到对应链表中
    cameras = QCameraInfo::availableCameras();
    for(auto e : cameras)
    {
        qDebug() << e.description();
    }
}

//修改曝光补偿（未通过测试）
bool MyQcamera::setExposure(int ValueEV, SetAorMKind mode)
{
    //获取到摄像头的曝光控制对象
    exposure = camera->exposure();
    //检查当前摄像头是否支持曝光修改

    if(exposure->isAvailable())
    {
        //先将曝光改成需要的形式自动/手动
        if(mode == SetAorMKind::Auto)
        {
           exposure->setExposureMode(QCameraExposure::ExposureAuto);
           return true;
        }else if(mode == SetAorMKind::Manual)
        {
            qDebug() << "曝光模式修改为手动";
            exposure->setExposureMode(QCameraExposure::ExposureManual);
        }else{
            ELOG("输入曝光模式错误");
            return false;
        }
        //设置曝光值
        exposure->setExposureCompensation((qreal)1.5);
        //修改曝光时间
//        qreal exposureTime = 1.0/500;
//        exposure->setManualShutterSpeed(exposureTime);
//        qDebug() << "修改曝光时长为:  " << exposureTime << "camera值为: " << camera;
        // 设置快门速度为 1/1000 秒
//        QCameraExposure::ShutterSpeed shutterSpeed = QCameraExposure::ShutterSpeed::Exposure_1_1000;
//        exposure->setShutterSpeed(shutterSpeed);
    }

    return true;
}

QCameraViewfinder *MyQcamera::RViewfinder()
{
    return viewfinder;
}

bool MyQcamera::switchCamera(const QString &cameraInfo)
{
    //qDebug() << (tr("更改为: %1").arg(cameraInfo));
    if(cameraInfo.isEmpty())
    {
        ELOG("cameraInfo不能为空");
        return false;
    }

    qDebug() << "1 " << currentCameraInfo->description();
    if(cameraInfo == currentCameraInfo->description())
    {
        return true;
    }
    qDebug() << 2;
    QCameraInfo *tmpInfo = nullptr;
    for(int i = 0; i < cameras.count(); i++)
    {
        if(cameraInfo.compare(cameras[i].description()) == 0)
        {
            tmpInfo = &cameras[i];
            break;
        }
    }
    if(tmpInfo == nullptr)
    {
        ELOG("传入cameraInfo错误!!");
        return false;
    }else
    {
        //ELOG("成功替换cameraInfo!!");
        *currentCameraInfo = *tmpInfo;
    }
    //qDebug() << (tr("当前摄像头为: %1,").arg(currentCameraInfo->description()));
    //先释放掉前面new的内存，再重新new一个新的QCamera
    camera->stop();
    camera->deleteLater();
    //delete camera;
    camera = new QCamera(*currentCameraInfo);
    //用于将摄像头的预览视图设置为指定的视图部件，以便实时显示摄像头捕获到的图像
    camera->setViewfinder(viewfinder);
    connect(camera, SIGNAL(stateChanged(QCamera::State)), this, SLOT(cameraStat(QCamera::State)));
    //启动摄像头
    camera->start();


    return true;
}
